<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Task_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();

	}
	
	function create_task($data)
	{
		$insert = $this->db->insert('tasks',$data);		
		return $this->db->insert_id($insert);		
	}
	
	public function get_my_tasks()
	{
		return $this->db->get_where('tasks',array('user_id'=>$this->session->userdata('user_id')),5)->result();
	}
	
	public function get_completed_tasks()
	{
		return $this->db->get_where('tasks',array('user_id'=>$this->session->userdata('user_id'),'is_completed'=>'1'))->result();
	}
	
	public function get_achieved_tasks()
	{
	return $this->db->get_where('tasks',array('user_id'=>$this->session->userdata('user_id'),'is_achieved'=>'1'))->result();	
	}
	
	public function get_pending_tasks()
	{
	return $this->db->get_where('tasks',array('user_id'=>$this->session->userdata('user_id'),'is_completed'=>'0','is_achieved'=>'0'))->result();	
	}
	
	
	public function get_task($id)
	{
	return $this->db->get_where('tasks',array('id'=>$id))->row_array();	
	}
	
	public function get_subtask($task_id)
	{
	return $this->db->get_where('sub_tasks',array('task_id'=>$task_id))->result();	
	}
	
	
	public function add_note($insert)
	{
		$this->db->insert('task_notes',$insert);
	}
	
	public function get_note_by_id($id)
	{
		$this->db->select('*');
		$this->db->from('task_notes');
		$this->db->where('task_id ='.$id);
		$this->db->join('users', 'task_notes.user_id = users.id');
		
	 return $this->db->get()->result();	
	}
	
	public function edit($edit_id)
	{
		$this->db->select('tasks.*,users.username');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$edit_id);
		$this->db->join('users', 'tasks.user_id = users.id');		
	 return $this->db->get()->row_array();	
		
		/*return $this->db->get_where('tasks',array('id'=>$edit_id))->row_array();*/	
		
	}
	
	public function update($update,$edit_id)
	{
		$this->db->update('tasks',$update,array('id'=>$edit_id));
	}
	
	public function is_achieved($update,$task_id)
	{
	$this->db->update('tasks',$update,$task_id);	
	}
	
	public function task_unachieved($update,$task_id)
	{
		//echo $update,$task_id;
		//exit;
		$this->db->update('tasks',$update,$task_id);
	}
	
	public function check_is_achieved($task_id)
	{
		$this->db->select('is_achieved');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$task_id);
		return $this->db->get()->row_array();
		//return $this->db->get_where('tasks',array('id'=>$task_id))->row_array();		
	}
	
	public function check_is_completed($task_id)
	{
		$this->db->select('is_completed');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$task_id);
		return $this->db->get()->row_array();
	}
	
	public function task_closed($update,$task_id)
	{
	$this->db->update('tasks',$update,$task_id);	
	}
	
	public function task_open($update,$task_id)
	{
		$this->db->update('tasks',$update,$task_id);
	}
	
	/* sub tasks functions */
	
	public function create_subtask($insert)
	{
		$this->db->insert('sub_tasks',$insert);
	}
	
	public function subtask_achieved($subtask_id)
	{		
		$this->db->update('sub_tasks',array('is_achieved'=>1),array('id'=>$subtask_id));
	}
	
	public function subtask_unachieved($subtask_id)
	{		
		$this->db->update('sub_tasks',array('is_achieved'=>0),array('id'=>$subtask_id));
	}
	
	public function check_subtask_is_achieved($subtask_id)
	{
		$this->db->select('is_achieved');
		$this->db->from('sub_tasks');
		$this->db->where('sub_tasks.id ='.$subtask_id);
		return $this->db->get()->row_array();
	}
	public function delete_subtask($delete_id)
	{
		$this->db->delete('sub_tasks',array('id'=>$delete_id));
	}
	
	/* Project function starts here */ 
	public function create_project($insert)
	{
		$this->db->insert('projects',$insert);
	}
	
	public function get_all_projects()
	{
		$this->db->select('projects.*,users.username');
		$this->db->from('projects');		
		$this->db->join('users', 'projects.created_by = users.id');	
			
		return $this->db->get()->result();
	}
	/* Project function end here */ 
	
	/* Milestone function end here */ 
	
	public function get_project_name_by_id($project_id)
	{
		return $this->db->get_where('projects',array('id'=>$project_id))->row_array();
	}
	
	public function create_milestone($insert)
	{
		$this->db->insert('milestones',$insert);
	}
	
	public function get_milestone_by_project_id($project_id)
	{
		$this->db->select('milestones.*,users.username');
		$this->db->from('milestones');
		$this->db->where('project_id ='.$project_id);
		$this->db->join('users', 'milestones.created_by = users.id');		
	 return $this->db->get()->result();
	}
	
	public function get_milestones()
	{		
	return $this->db->get('milestones')->result();
	}
	
	public function get_default_milestone()
	{
		return $this->db->get_where('milestones',array('is_default'=> 1))->result();
	}
	
	public function get_taskgroups()
	{		
	return $this->db->get('task_groups')->result();
	}
	
	public function create_taskgroups($insert)
	{
		$this->db->insert('task_groups',$insert);
	}
	
}
